/**
 * utils
 * @author yangjian102621@gmail.com
 */
#include "utils.h"

/**
 * remove specified char from string
 * @param str
 * @param ch
 * @return
 */
char * right_trim(char *str, char ch)
{
	char * p = str + strlen(str) - 1;
	if (str == NULL || *str == '\0') {
		return str;
	}

	while (p >= str  && *p == ch) {
		*p = '\0';
		p--;
	}

	return str;
}

/**
 * remove specified char at left
 * @param str
 * @param ch
 * @return
 */
char * left_trim(char *str, char ch)
{
	int len = 0;
	char *p = str;

	if (str == NULL || *str == '\0') {
		return str;
	}
	while (*p != '\0' && *p == ch)
	{
		++p;
		++len;
	}
	memmove(str, p, strlen(str) - len + 1);
	return str;
}

/**
 * remove specified char at right
 * @param str
 * @param ch
 * @return
 */
char * trim(char *str, char ch)
{
	str = left_trim(str, ch);
	str = right_trim(str, ch);
	return str;
}